package com.msh.starter.qrcode.instance;

import com.msh.frame.client.exception.ServiceException;
import com.msh.frame.interfaces.IQrCode;
import com.msh.starter.qrcode.config.QrCodeProperties;
import com.msh.starter.qrcode.util.QrCodeUtil;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@AllArgsConstructor
public class QrCodeWithLogoGenerator implements IQrCode {
  private QrCodeProperties qrCodeProperties;
  private BufferedImage logo;



  @Override
  public void outputStream(String text, OutputStream os) {
    try {
      BufferedImage bufferedImage = QrCodeUtil
          .generateQrCodeWithLogo(text, logo, qrCodeProperties.getWidth(), qrCodeProperties.getHeight(), qrCodeProperties.getCodeType(), qrCodeProperties.getErrorCorrectionLevel());
      ImageIO.write(bufferedImage, qrCodeProperties.getFormat(), os);
    } catch (Exception e) {
      log.error("QrCodeGenerator outputStream error", e);
      ServiceException.doThrow("二维码生成失败");
    }
  }
}
